﻿# More info at http://psautodeploy.codeplex.com
# Author: SPFernando

Import-Module .\Utils.ps1

# Deactivate Features
function DisableFeature {
	param(
		[parameter(Position=0, Mandatory=$true)]
		[string]$FeatureName, 
		[parameter(Position=1, Mandatory=$true)]
		[string]$Url
	)
		
	if ((Get-SPFeature -Web $Url |? {$_.DisplayName -eq $FeatureName}) -or (Get-SPFeature -Site $Url |? {$_.DisplayName -eq $FeatureName})) {		
		Write-Host "`n`tDisabling existing feature" -ForegroundColor "DarkCyan"
		LogWrite "TRACE - Disabling Feature $FeatureName"
		Disable-SPFeature -Identity $FeatureName -Url $Url -Force -Confirm:$false
		LogWrite "TRACE - Disabled Feature $FeatureName"
	}
}
# Uninstall Features
function UninstallFeature {
	param(
		[parameter(Position=0, Mandatory=$true)]
		[string]$FeatureName,
		[parameter(Position=1, Mandatory=$true)]
		[string]$Url
	)

	Write-Host "`n`n`t$FeatureName" -ForegroundColor "Green"
	$UnderLine = "-"
	$UnderLine = $UnderLine * $FeatureName.Length
	Write-Host "`n`t$UnderLine`n" -ForegroundColor "Green"
	
	DisableFeature $FeatureName $Url
	Write-Host "`n`tRemoving feature" -ForegroundColor "DarkCyan"
	LogWrite "TRACE - Removing Feature $FeatureName"
	Uninstall-SPFeature $FeatureName -Force -Confirm:$false
	LogWrite "TRACE - Removed Feature $FeatureName"
}

# Remove Features
function RemoveFeature {
	param(
		[parameter(Position=0, Mandatory=$true)]
		[Microsoft.SharePoint.SPSite]$Site, 
		[parameter(Position=1, Mandatory=$true)]
		[string]$SolutionName
	)

	try {
		$ErrorActionPreference = 'Stop'
		$SolutionToCheck = Get-SPSolution -Identity $SolutionName
		
		foreach($Web in $Site.AllWebs) {
			foreach ($grp in Get-SPFeature | Group-Object SolutionId |?{$_.Name -eq $SolutionToCheck.Id}) {
				foreach ($Feature in $grp.Group |?{$_.Scope -eq "Web"} | Sort-Object $Feature.DisplayName) {		
					UninstallFeature $Feature.DisplayName $Web.Url
				}
			}
		}

		foreach ($grp in Get-SPFeature | Group-Object SolutionId |? {$_.Name -eq $SolutionToCheck.Id}) {
			foreach ($Feature in $grp.Group |?{$_.Scope -ne "Web"} | Sort-Object $Feature.DisplayName) {		
				UninstallFeature $Feature.DisplayName $Site.Url
			}
		}
	}
	catch {
		# Show & Log Error
		LogWrite "ERROR - RemoveFeature - $_.Exception.Message"
		Write-Error "`n$_.Exception.Message." 		
		KeyDown
	}
}

# Activate Feature
function ActivateFeature {
	param(
		[parameter(Position=0, Mandatory=$true)]
		[string]$FeatureName, 
		[parameter(Position=1, Mandatory=$true)]
		[string]$Url,
		[parameter(Position=2, Mandatory=$false)]
		[bool]$Auto=$false
	)
	$FeatureActivate = Get-SPFeature $FeatureName	
	
	if ($Auto -eq $false -or $FeatureActivate.ActivateOnDefault) {	
		DisableFeature $FeatureName $Url
		Write-Host "`n`tActivating feature`n" -ForegroundColor "DarkCyan"
		LogWrite "TRACE - Activating Feature $FeatureName"
	    Enable-SPFeature -Identity $FeatureName -Url $Url -Force | Out-Null
		LogWrite "TRACE - Activated Feature $FeatureName"	
	}
	else {
		Write-Host "`n`tFeature not activated because ActivateOnDefault's Feature property returns false.`n" -ForegroundColor "DarkCyan"
		LogWrite "TRACE - No Activating Feature $FeatureName"
	}
}

# Install Feature
function InstallFeature {
	param(	
		[parameter(Position=0, Mandatory=$true)]
		[string]$FeatureName,
		[parameter(Position=1, Mandatory=$true)]
		[string]$Url,
		[parameter(Position=2, Mandatory=$false)]
		[bool]$Auto=$false
	)

	Write-Host "`n`n`t$FeatureName" -ForegroundColor "Green"
	$UnderLine = "-"
	$UnderLine = $UnderLine * $FeatureName.Length
	Write-Host "`n`t$UnderLine`n" -ForegroundColor "Green"
	
    if ($FeatureName -ne $null) {       
		Write-Host "`n`tInstalling feature" -ForegroundColor "DarkCyan"		
		LogWrite "TRACE - Installing Feature $FeatureName"

        Install-SPFeature $FeatureName -Force | Out-Null
        LogWrite "TRACE - Installed Feature $FeatureName"
        
        ActivateFeature $FeatureName $Url $Auto
    }
}

# Install Features (for WebFeatures across all children Webs inside Site)
function AddFeatures {
	param(
		[parameter(Position=0, Mandatory=$true)]
		[Microsoft.SharePoint.SPSite]$Site, 
		[parameter(Position=1, Mandatory=$true)]
		[string]$SolutionName,
		[parameter(Position=2, Mandatory=$false)]
		[bool]$Auto=$false
	)

	try {
		$ErrorActionPreference = 'Stop'
		$SolutionToCheck = Get-SPSolution -Identity $SolutionName

		foreach ($grp in Get-SPFeature | Group-Object SolutionId |? {$_.Name -eq $SolutionToCheck.Id}) {
			foreach ($Feature in $grp.Group |?{$_.Scope -ne "Web"} | Sort-Object $Feature.DisplayName) {		
				InstallFeature $Feature.DisplayName $Site.Url $Auto
			}
		}
		foreach($Web in $Site.AllWebs) {
			foreach ($grp in Get-SPFeature | Group-Object SolutionId |?{$_.Name -eq $SolutionToCheck.Id}) {
				foreach ($Feature in $grp.Group |?{$_.Scope -eq "Web"} | Sort-Object $Feature.DisplayName) {				
					InstallFeature $Feature.DisplayName $Web.Url $Auto
				}
			}
		}
	}
	catch {
		# Show & Log Error
		LogWrite "ERROR - AddFeatures - $_.Exception.Message"
		Write-Error "`n$_.Exception.Message." 		
		KeyDown
	}
}